package com.devdan.headsetfix;

import java.util.ArrayList;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import static com.devdan.headsetfix.HeadsetCommonConstants.*;

public class ContactPickerActivity extends   ListActivity{
    private static final int CONTACT_PICKER_RESULT = 1001;

    
	public void onCreate(Bundle savedInstanceState) {    	
	        super.onCreate(savedInstanceState);
//	        setContentView(R.layout.contactpicker);
	    	 Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);  
	    	 startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);	        
	}

	// Hint: If this returns directly after contact picker was started and not after
	// user has actually chosen a contact, make sure your activity uses android:launchMode="singleTop"
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
	    if (resultCode == RESULT_OK) {  
	    	if (requestCode == CONTACT_PICKER_RESULT) {
	    		ArrayList<String> numberList = new ArrayList<String>(); 
	    		Uri result = data.getData();   		
	    		String contactID = result.getLastPathSegment();
	              // query for everything phone  
	              Cursor cursor = getContentResolver().query(  
	                      Phone.CONTENT_URI, null,  
	                      Phone.CONTACT_ID + "=?",  
	                      new String[]{contactID}, null); 
	              startManagingCursor(cursor);
	              cursor.moveToFirst(); 
	              
	              Cursor phones = this.getContentResolver().query(
	                      Phone.CONTENT_URI, null,
	                      Phone.CONTACT_ID + " = " + contactID, null, null);
	              while (phones.moveToNext()) {
	            	  String numberType;
	                  String number = phones.getString(phones
	                          .getColumnIndex(Phone.NUMBER));
	                  int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
	                  switch (type) {
	                  case Phone.TYPE_HOME:                   
	                       numberType = new String("home:   ");
	                      break;
	                  case Phone.TYPE_MOBILE:                 
	                       numberType = new String("mobile: ");
	                       break;
	                  case Phone.TYPE_WORK:               
	                       numberType = new String("work:   ");	                	  
	                      break;
	                  default:
	                       numberType = new String("other:  ");	                	  
	                      break;
	                  }	                  
	                  if (!numberList.contains(numberType + number)) {
	                	  numberList.add(numberType + number);
	                  }

	              }	              
				
			String[] values = numberList.toArray(new String[numberList.size()]);

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
			ListView myList = (ListView) getListView();

			myList.setAdapter(adapter);			
	    	myList.setOnItemClickListener(new OnItemClickListener() {
			    public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
			      // When clicked, show a toast with the TextView text
//			     Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
//			         Toast.LENGTH_SHORT).show();
		    	Intent resultIntent = new Intent();
		    	// extract number (on the right side of the colon) and remove white spaces
		    	String result =  ((String) ((TextView) view).getText()).split(":")[1].trim();
		    	resultIntent.putExtra(KEY_SELECTED_PHONE_NUMBER,result);
		    	setResult(Activity.RESULT_OK, resultIntent);
		    	finish();				     
			   }
    				    
			 });
    	}

	    }  
	}	  
}
